package com.android.panballgame;

public class PBOverlapTester {
    public static boolean overlapCircles(PBCircle c1, PBCircle c2) {
        float distance = c1.center.distSquared(c2.center);
        float radiusSum = c1.radius + c2.radius;
        return distance <= radiusSum * radiusSum;
    }
      
    public static int overlapCircleRectangle(PBCircle c, PBRectangle r) {
    	int num = 0;
    	
        float closestX = c.center.x;
        float closestY = c.center.y;
        
        if(c.center.x < r.lowerLeft.x) {
            closestX = r.lowerLeft.x; 
        } 
        else if(c.center.x > r.lowerLeft.x + r.width) {
            closestX = r.lowerLeft.x + r.width;
        }          
        if(c.center.y < r.lowerLeft.y) {
            closestY = r.lowerLeft.y;
        } 
        else if(c.center.y > r.lowerLeft.y + r.height) {
            closestY = r.lowerLeft.y + r.height;
        }
        
        if(c.center.distSquared(closestX, closestY) < c.radius * c.radius){
        	if(closestX == r.lowerLeft.x && closestY == r.lowerLeft.y){
        		num = 1;
        	}else	if(closestX == r.lowerLeft.x && closestY == c.center.y){
        		num = 2;
        	}else	if(closestX == r.lowerLeft.x && closestY == r.lowerLeft.y + r.height){
        		num = 3;
        	}else	if(closestX == c.center.x && closestY ==  r.lowerLeft.y){
        		num = 4;
        	}else	if(closestX == c.center.x && closestY == c.center.y){
        		num = 5;
        	}else	if(closestX == c.center.x && closestY == r.lowerLeft.y + r.height){
        		num = 6;
        	}else	if(closestX == r.lowerLeft.x + r.width && closestY == r.lowerLeft.y){
        		num = 7;
        	}else	if(closestX == r.lowerLeft.x + r.width && closestY == c.center.y){
        		num = 8;
        	}else	if(closestX == r.lowerLeft.x + r.width && closestY == r.lowerLeft.y + r.height){
        		num = 9;
        	}       		
        }else{
        	num = 0;
        }     	
        return num;        
    }
    
    public static boolean pointInCircle(PBCircle c, PBVector p) {
        return c.center.distSquared(p) < c.radius * c.radius;
    }
    
    public static boolean pointInCircle(PBCircle c, float x, float y) {
        return c.center.distSquared(x, y) < c.radius * c.radius;
    }
    
    public static boolean pointInRectangle(PBRectangle r, PBVector p) {
        return r.lowerLeft.x <= p.x && r.lowerLeft.x + r.width >= p.x &&
               r.lowerLeft.y <= p.y && r.lowerLeft.y + r.height >= p.y;
    }
    
    public static boolean pointInRectangle(PBRectangle r, float x, float y) {
        return r.lowerLeft.x <= x && r.lowerLeft.x + r.width >= x &&
               r.lowerLeft.y <= y && r.lowerLeft.y + r.height >= y;
    }
}